<template>
    <div>
         <header class="header" >
  <div class="navbar" style="display:flex;flex-direction:row;justify-content:space-between;align-items:center;">
    <div class="navbar-left-container;line-height:50px">
      <a href="/">
        <img class="navbar-brand-logo" src="../../assets/images/logo1.png"></a>
    </div>
    <div class="navbar-right-container" style="display: flex">
      <div class="navbar-menu-container" style="display:flex;flex-direction:row;align-items:center;">
        <!--<a href="/" class="navbar-link">我的账户</a>-->
        <span class="navbar-link">{{this.$store.state.userName}}</span>
        <a href="javascript:void(0)" class="navbar-link" style="padding:10px" @click="open()" v-if="showlogin">Login</a>
        <a href="javascript:void(0)" class="navbar-link" style="padding:10px" @click="logout()" v-if="!showlogin">Logout</a>
        <div class="navbar-cart-container" style="padding:10px">
          <el-badge :value="cartNum" class="item" style="margin-right:20px">
          <router-link class="navbar-link navbar-cart-link" to="/cart">
            <img src="../../assets/images/cart.png" style="width:30px;height:30px">
          </router-link>
          </el-badge>
        </div>
      </div>
    </div>
  </div>
  <div class="md-modal modal-msg md-modal-transition" :class="{'md-show':show}">
          <div class="md-modal-inner">
            <div class="md-top">
              <div class="md-title">Login in</div>
              <button class="md-close" @click="close()">Close</button>
            </div>
            <div class="md-content">
              <div class="confirm-tips">
                <div class="error-wrap">
                  <span class="error error-show" v-if="errorTip">用户名或者密码错误</span>
                </div>
                <ul>
                  <li class="regi_form_input">
                    <i class="icon IconPeople"></i>
                    <input type="text" tabindex="1" name="loginname" v-model="username" class="regi_login_input regi_login_input_left" placeholder="User Name" data-type="loginname">
                  </li>
                  <li class="regi_form_input noMargin">
                    <i class="icon IconPwd"></i>
                    <input type="password" tabindex="2"  name="password" v-model="password" class="regi_login_input regi_login_input_left login-input-no input_text" placeholder="Password" @keyup.enter="login">
                  </li>
                </ul>
              </div>
              <div class="login-wrap">
                <a href="javascript:;" class="btn-login" @click="login()">登  录</a>
              </div>
            </div>
          </div>
        </div>
        <div class="md-overlay" v-if="show"></div>
</header>
    </div>
</template>
<script>
  import '../../assets/css/login.css'
export default {
    name:'Hander',
    data(){
        return{
          show:false,
          username:'',
          password:'',
          errorTip:false,
          loginname:'',
          showlogin:true,
          userId:''
        }
    },
    methods:{
      close(){
        this.show = false;
      },
      open(){
        this.show = true;
      },
      login(){
        if(!this.username || !this.password){
          this.errorTip = true
        }
        this.axios.defaults.withCredentials = true
        this.axios.post('http://127.0.0.1:3333/user/login',{username:this.username,password:this.password}).then((data)=>{
          console.log(data);
          if(data.data.status=='0'){
            this.show = false;
            this.loginname = data.data.result.username;
            this.userId = data.data.result.userId;
            this.showlogin = false;
            this.$store.commit('setUserName',data.data.result.username);
            this.init();
          }else{
            console.log(1);
          }
        }).catch((err)=>{
          console.log(err);
        })
      },
      logout(){
        this.axios.defaults.withCredentials = true
        this.axios.post('http://127.0.0.1:3333/user/logout').then((data)=>{
          console.log(data);
          if(data.data.status=='0'){
            this.loginname = ''
            this.showlogin = true;
            this.$store.commit('setUserName', "");
          }
        })
      },
      init(){
        this.axios.get('http://127.0.0.1:3333/getCartNum').then((res)=>{
          //console.log(res);
          if(res.data.status=='0'){
            this.$store.commit('setCartNum',res.data.result);
          }
        }).catch((err)=>{
          console.log(err);
        })
      }
    },
    mounted(){
      this.axios.defaults.withCredentials = true;
      this.axios.post('http://127.0.0.1:3333/checkLogin').then((data)=>{
        if(data.data.status == '0'){
             console.log(data);
            this.loginname = data.data.result.userName;
            this.userId = data.data.result.userId;
            this.showlogin = false;
            this.init();
            this.$store.commit('setUserName',data.data.result.userName);
        }
     
      });
      
    },
    computed:{
      cartNum(){
        return this.$store.state.cartNum;
      }
    }
}
</script>
<style scoped>

</style>